#include "EndGame.h"

// Initialize the instance to null
EndGame* EndGame::m_EndGameInstance = 0;

EndGame::EndGame(void)
{
}

EndGame::~EndGame(void)
{
}

EndGame* EndGame::GetInstance(void)
{
	// Create the singleton if it doesn't exit
	if(!m_EndGameInstance)
		m_EndGameInstance = new EndGame;

	// Return the singleton
	return m_EndGameInstance;
}

void EndGame::DeleteEndGameInstance(void)
{
	// If instance exist, delete it
	if(m_EndGameInstance)
	{
		delete m_EndGameInstance;
		m_EndGameInstance = NULL;
	}
}

void EndGame::Init(IDirect3DDevice9* m_Device, HWND hWnd, HINSTANCE hInst, DirectInput* directInput, int nWidth, int nHeight)
{
	// Get the instance and initialize the sprite manager
	m_pSpriteManager = SpriteManager::GetInstance();

	// Initialize sound variable to default constructor
	m_pSound = Sound::GetSoundInstance();

	// Initialize text variable to default constructor
	m_pText = Text::GetInstance();

	// Initialize HighScore variable to default constructor
	m_pHighScore = HighScore::GetInstance();
	m_pHighScore->ReadFromFile();
	m_pHighScore->SortBoard();
	m_pHighScore->WriteToFile();
	
	hWin = hWnd;
	m_pDirectInput = directInput;
}

void EndGame::LoadEndGameSprites(void)
{
	sEndGame = JUSTENDED;
	sGameState = 3;
	// Sprite for the End Screen
	sEndScreen = m_pSpriteManager->LoadSprite("EndGameSprites\\victoryScreen.png", D3DXVECTOR3(512, 200, 0), 0.8f);
	sSubmitScoreScreen = m_pSpriteManager->LoadSprite("EndGameSprites\\submitScoreScreen.png", D3DXVECTOR3(512, 384, 0), 1.0f);	
}

void EndGame::Update(HWND hWnd, HINSTANCE hInst, float deltaTime)
{
	m_pDirectInput->Update(hWnd);
	switch(sEndGame)
	{
	case JUSTENDED:
 		if(m_pDirectInput->CheckKeyboard(DIK_RETURN))
		{
			//Check if a new highscore can be entered. If not return to main menu.
			if( sTotalScore > m_pHighScore->Scores[9].sScores )
			{
				bPass = false;
				sEndGame = GETTINGNAME;
			}
			else
				sGameState = 0;
		}
		break;
	case GETTINGNAME:
		break;
	}
}

void EndGame::Render(void)
{
	switch(sEndGame)
	{
	case JUSTENDED:
		// Render the End Screen Sprite
		m_pSpriteManager->DrawSprite(sEndScreen);
		break;
	case GETTINGNAME:
		// Render the Submit Score Screen Sprite
		m_pSpriteManager->DrawSprite(sSubmitScoreScreen);
		//Allow user to type in thier name then submit it and thier score to the highscore text file.
		if( m_pHighScore->GetUserName(hWin) )
		{
			m_pHighScore->Scores[9].sScores = sTotalScore;
			stringstream out;
			out << m_pHighScore->Scores[9].sScores;
			m_pHighScore->Scores[9].strScore = out.str();

			m_pHighScore->Scores[9].strName = m_pHighScore->strLName;
			m_pHighScore->SortBoard();
			m_pHighScore->WriteToFile();
			bPass = true;
			sGameState = 0;
		}
		break;
	}
}
short EndGame::GetScore(short sUserScore)
{
	//Get users score from gameplay
	sTotalScore = sUserScore;
	return sTotalScore;
}
short EndGame::GetStateEndGame(void)
{
	return sGameState;
}

void EndGame::Shutdown(void)
{
	// Destroy all sprites
	m_pSpriteManager->DestroySprite(sEndScreen);
	m_pSpriteManager->DestroySprite(sSubmitScoreScreen);
}